package com.yibo.yunpicture.interfaces.controller;

import com.yibo.yunpicture.infrastructure.common.BaseResponse;
import com.yibo.yunpicture.infrastructure.common.ResultUtils;
import com.yibo.yunpicture.infrastructure.exception.ErrorCode;
import com.yibo.yunpicture.infrastructure.exception.ThrowUtils;
import com.yibo.yunpicture.interfaces.dto.space.analyze.*;
import com.yibo.yunpicture.interfaces.vo.space.analyze.*;
import com.yibo.yunpicture.domain.space.entity.Space;
import com.yibo.yunpicture.domain.user.entity.User;
import com.yibo.yunpicture.application.service.SpaceAnalyzeApplicationService;
import com.yibo.yunpicture.application.service.UserApplicationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/space/analyze")
public class SpaceAnalyzeController {

    @Resource
    private SpaceAnalyzeApplicationService spaceAnalyzeApplicationService;

    @Resource
    private UserApplicationService userApplicationService;

    /**
     * 获取空间使用状态
     */
    @PostMapping("/usage")
    public BaseResponse<SpaceUsageAnalyzeVO> getSpaceUsageAnalyze(@RequestBody SpaceUsageAnalyzeDTO spaceUsageAnalyzeDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUsageAnalyzeDTO == null , ErrorCode.PARAMS_ERROR , "请求空间使用状态参数为空");
        User loginUser = userApplicationService.getLoginUser(request);
        ThrowUtils.throwIf(loginUser == null , ErrorCode.NOT_LOGIN_ERROR , "用户未登录");
        SpaceUsageAnalyzeVO spaceUsageAnalyzeVO = spaceAnalyzeApplicationService.getSpaceUsageAnalyze(spaceUsageAnalyzeDTO, loginUser);
        return ResultUtils.success(spaceUsageAnalyzeVO);
    }

    /**
     * 获取空间分类使用状态
     */
    @PostMapping("/category")
    public BaseResponse<List<SpaceCategoryAnalyzeVO>> getSpaceCategoryAnalyze(@RequestBody SpaceCategoryAnalyzeDTO spaceCategoryAnalyzeDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceCategoryAnalyzeDTO == null , ErrorCode.PARAMS_ERROR , "请求空间分类使用状态参数为空");
        User loginUser = userApplicationService.getLoginUser(request);
        List<SpaceCategoryAnalyzeVO> spaceCategoryAnalyzeVOList = spaceAnalyzeApplicationService.getSpaceCategoryAnalyze(spaceCategoryAnalyzeDTO, loginUser);
        return ResultUtils.success(spaceCategoryAnalyzeVOList);
    }

    /**
     * 获取空间标签使用状态
     */
    @PostMapping("/tag")
    public BaseResponse<List<SpaceTagAnalyzeVO>> getSpaceTagAnalyze(@RequestBody SpaceTagAnalyzeDTO spaceTagAnalyzeDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceTagAnalyzeDTO == null, ErrorCode.PARAMS_ERROR, "请求空间标签使用参数为空");
        User loginUser = userApplicationService.getLoginUser(request);
        List<SpaceTagAnalyzeVO> spaceTagAnalyzeVOList = spaceAnalyzeApplicationService.getSpaceTagAnalyze(spaceTagAnalyzeDTO, loginUser);
        return ResultUtils.success(spaceTagAnalyzeVOList);
    }

    /**
     * 获取空间图片大小分布
     */
    @PostMapping("/size")
    public BaseResponse<List<SpaceSizeAnalyzeVO>> getSpaceSizeAnalyze(@RequestBody SpaceSizeAnalyzeDTO spaceSizeAnalyzeDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceSizeAnalyzeDTO == null, ErrorCode.PARAMS_ERROR, "请求空间图片大小参数为空");
        User loginUser = userApplicationService.getLoginUser(request);
        List<SpaceSizeAnalyzeVO> spaceSizeAnalyzeVOList = spaceAnalyzeApplicationService.getSpaceSizeAnalyze(spaceSizeAnalyzeDTO, loginUser);
        return ResultUtils.success(spaceSizeAnalyzeVOList);
    }

    /**
     * 获取空间用户分析
     */
    @PostMapping("/user")
    public BaseResponse<List<SpaceUserAnalyzeVO>> getSpaceUserAnalyze(@RequestBody SpaceUserAnalyzeDTO spaceUserAnalyzeDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUserAnalyzeDTO == null, ErrorCode.PARAMS_ERROR, "请求空间用户分析参数为空");
        User loginUser = userApplicationService.getLoginUser(request);
        List<SpaceUserAnalyzeVO> spaceUserAnalyzeVOList = spaceAnalyzeApplicationService.getSpaceUserAnalyze(spaceUserAnalyzeDTO, loginUser);
        return ResultUtils.success(spaceUserAnalyzeVOList);
    }

    /**
     * 空间使用排行分析
     */
    @PostMapping("/rank")
    public BaseResponse<List<Space>> getSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeDTO spaceRankAnalyzeDTO, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceRankAnalyzeDTO == null, ErrorCode.PARAMS_ERROR, "请求空间使用排行分析参数为空");
        User loginUser = userApplicationService.getLoginUser(request);
        List<Space> spaceList = spaceAnalyzeApplicationService.getSpaceRankAnalyze(spaceRankAnalyzeDTO, loginUser);
        return ResultUtils.success(spaceList);
    }
}
